from tkinter import TOP, LEFT, StringVar
from tkinter.messagebox import showinfo
from tkinter.ttk import Button, Frame, Label, Entry
from taobao_check.tools import ENTOZH


class AccountTab(Frame):
    def __init__(self, version, config):
        Frame.__init__(self)
        self.timeout = 30
        self.version = version
        self.config = config
        self.account = {}
        self.init_tab()
        self.pack()

    def init_tab(self):
        tip_label = Label(self, text="修改账号信息后需要保存才能生效！", font=("微软雅黑", 10), foreground="red")
        tip_label.pack(side=TOP, anchor='center', padx=20, pady=20)
        for name, info in self.config.account_zh.items():
            if name not in self.config.user.account:
                continue
            user, pwd = StringVar(), StringVar()
            if name == "用户名":
                user.set(info)
                self.account[name] = user
            else:
                user.set(info[0])
                pwd.set(info[1])
                self.account[name] = [user, pwd]
            row_frame = Frame(self)
            Label(row_frame, text=f"{name:<8}:" if len(name)==3 else f"{name:<6}:").pack(side=LEFT, padx=20)
            Entry(row_frame, textvariable=user).pack(side=LEFT, padx=20)
            if name != "用户名":
                Entry(row_frame, textvariable=pwd).pack(side=LEFT, padx=20)
            row_frame.pack(side=TOP, padx=20, pady=5)

        save_btn = Button(self, text="保存", cursor='hand2', width=10, command=self.save_account)
        save_btn.pack(side=TOP, anchor='center', padx=50, pady=20)

    def save_account(self):
        for name, info in self.account.items():
            if name == "用户名":
                self.config.account_zh[name] = info.get().strip()
            else:
                user, pwd = info
                self.config.account_zh[name] = [user.get().strip(), pwd.get().strip()]
        self.config.save_account()
        showinfo(title="成功", message="账号信息保存成功！")

